<!--
* description: 备件库存查询
* author: ligy
* createdDate: 2021-3-4
-->

<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      dynamic-is-show-more-btn="true"
      :dynamic-is-init-table="true"
      :dynamic-api-style="'restful'"
    />

    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :dynamic-api-style="'restful'"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/pa/apiBranchWarehouse'
import { exportExcel } from '@/utils/getTimeString.js'
import Vue from 'vue'

export default {
  name: 'SparePartsInventoryInquiry',

  components: {
    OneTableTemplate
    // Edit: () => import('./edit')
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      editRowData: {},
      // 网格查询API配置对象
      apiConfig: api.sparePartsInventoryQueryService,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'comKey2',
          type: '',
          size: 'small',
          name: '',
          position: 'left',
          clickEvent: () => this.exportExcel111(),
          text: '导出',
          disabled: false
        },
        {
          compKey: 'btnKey3',
          type: '',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '网点编码：',
              codeField: 'dlrCode',
              component: () =>
                import('@/components/org/commonInput/searchInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '网点名称：',
              codeField: 'dlrShortName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '仓库名称：',
              codeField: 'warehouseName',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '备件编码：',
              codeField: 'partNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '备件名称：',
              codeField: 'partName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '有不良品：',
              codeField: 'badProductsQty',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '有' },
                { statusId: '0', statusText: '无' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            // {
            //   compKey: 'compKey7',
            //   labelName: '适用车型：',
            //   codeField: 'carConfigIdList',
            //   component: () => import('@/components/pa/carName/index'),
            //   type: 'inputText',
            //   isMul: false,
            //   isMust: false
            // }
            {
              compKey: 'compKey7',
              labelName: '适用车型：',
              codeField: 'carConfigIdList',
              textField: 'carConfigCn',
              span: 6,
              component: () => import('@/views/ve/planReport/components/multipleSearchInput'),
              dataToObject: {
                oFields: 'carConfigCode,carConfigCn',
                // 控制弹窗页面 搜索 网格 元素
                tableComponents: [
                  {
                    compKey: 'compKey1',
                    labelName: '车型编码：',
                    codeField: 'carConfigCode',
                    isMul: false,
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  },
                  {
                    compKey: 'compKey2',
                    labelName: '车型名称：',
                    codeField: 'carConfigCn',
                    isMul: false,
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  }
                ],
                tableCols: [
                  {
                    prop: 'carSeriesCode',
                    label: '车系编码'
                  },
                  {
                    prop: 'carSeriesCn',
                    label: '车系名称'
                  },
                  {
                    prop: 'carConfigCode',
                    label: '车型编码'
                  },
                  {
                    prop: 'carConfigCn',
                    label: '车型名称'
                  }
                ],
                formField: {
                  carConfigCode: '',
                  carConfigCn: '',
                },
                returnField: {
                  prop: 'carConfigCode',
                  label: '车型编码'
                },
                api: { APIUrl: 'mp/e3sh/spareparts/basic/partPriceQueryService/queryCarConfigCodes.do', ServiceCode: 'queryCarConfigCodes' },
              },
              type: 'inputText',
              isMul: false,
              isMust: false
            }

          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'dlrId',
              label: '网点ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'partId',
              label: '备件ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'warehouseId',
              label: '仓库ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'dlrCode',
              label: '网点编码',
              width: '150px'
            },
            {
              prop: 'dlrShortName',
              label: '网点名称',
              width: '250px'
            },
            {
              prop: 'partNo',
              label: '备件编号',
              width: '150px'
            },
            {
              prop: 'partName',
              label: '备件名称'
            },
            {
              prop: 'partRepairTypeName',
              label: '备件类别'
            },
            {
              prop: 'carConfigIdList',
              label: '适用车型'
            },
            {
              prop: 'accountQty',
              label: '账面库存'
            },
            {
              prop: 'frozenQty',
              label: '冻结库存'
            },
            {
              prop: 'badProductsQty',
              label: '不良品',
              hidden: true
            },
            {
              prop: 'availableInventory',
              label: '可用库存'
            },
            {
              prop: 'pointPriceTax',
              label: '成本单价'
            },
            {
              prop: 'warehouseName',
              label: '仓库名称',
              width: 125
            },
            {
              prop: 'addressCode',
              label: '地址码',
              width: 125
            },
            {
              prop: 'inventoryAmount',
              label: '库存金额'
            },
            {
              prop: 'lastIndate',
              label: '最新有效入库日期',
              width: '150px'
            },
            {
              prop: 'lastOutdate',
              label: '最新有效出库日期',
              width: '150px'
            }
          ],

      // 表单查询数据
      formField: this.$utils.getFormField(
        this,
        {
          dlrCode: '',
          dlrShortName: '',
          partNo: '',
          partName: '',
          carConfigIdList: '', // 实际是 车型编码
          carConfigCn: '', // 车型名称
          badProductsQty: '', // 有无不良品
          warehouseName: ''
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      )
    }
  },
  watch: {
    'formField.carConfigCn': {
      deep: true,
      handler: function(newV, oldV) {
        if (newV == '' || newV == null) {
          this.formField.carConfigIdList = ''
        }
      }
    }
  },
  mounted() {
    // 列表查询
    this.queryTable(1)
    // 网点则禁用网点编码以及网点名称
    if(this.$store.getters.orgInfo.DLR_ID!=="HOST"){
      this.tableComponents.find(o=>o.codeField==="dlrCode").disabled=true
      this.tableComponents.find(o=>o.codeField==="dlrShortName").disabled=true
    } else {
      this.tableComponents.find(o=>o.codeField==="dlrCode").disabled=false
      this.tableComponents.find(o=>o.codeField==="dlrShortName").disabled=false
    }
    this.tableComponents.find(o=>o.codeField==="dlrCode").compKey=this.tableComponents.find(o=>o.codeField==="dlrCode").compKey+"1"
    this.tableComponents.find(o=>o.codeField==="dlrShortName").compKey=this.tableComponents.find(o=>o.codeField==="dlrShortName").compKey+"1"
  },

  methods: {
    // 导出
    exportExcel111() {
      // 导出
      exportExcel(
        this.formField,
        api.sparePartsInventoryexportDetails.APIUrl,
        api.sparePartsInventoryexportDetails.ServiceCode,
        '备件库存查询'
      )
    },
    // 编辑获取数据
    edit(index) {
      this.editPopupsState = 'edit'
      this.editPopupsVisible = true
      this.editRowData = this.$utils.deepCopy(
        this.$refs['multipleTable'].list[index]
      )
    },

    // 导出
    export() {
      var date = new Date()
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var day = date.getDate()
      month = month < 10 ? '0' + month : month
      day = day < 10 ? '0' + day : day
      var mydate = year.toString() + month.toString() + day.toString()
      const returnObj = {
        // api配置
        apiConfig: {
          APIUrl: '/mp/e3sh/basics/ve/CarTypeService/exportCarType.do'
        },
        variables: this.formField
      }
      returnObj.variables.dataType = 'excel'
      const paramD = this.$getParams(returnObj)
      this.$requestAPI(paramD).then((res) => {
        this.$utils.downloadFile(res, `车系维护${mydate}.xlsx`)
      })
    }
  }
}
</script>
